App.define("htmlTool", {
    getOptions: function (str) {
        var pattern = /<option\s*([^>]*?)\s*>([\S\s]*?)<\/option>/g,
            result = [];
        var matches = pattern.exec(str);
        while (matches) {
            var computed = this.getStrObj(matches[1]);
            computed.content = matches[2];
            result.push(computed);
            matches = pattern.exec(str);
        }
        return result;
    },
    getOptionAndOptGroup: function (str) {
        var parser = new DOMParser();
        var htmlDom = parser.parseFromString(str, "text/html");
        var result = [], body = htmlDom.body;
        for (var i = 0; i < body.children.length; i++) {
            var child = body.children[i];
            if (child.tagName.toUpperCase() == "OPTION") {
                result.push({
                    type: 1,
                    value: child.value,
                    content: child.textContent,
                    checked: child.getAttribute("checked") !== null,
                    disabled: child.getAttribute("disabled") !== null,
                    nodes: []
                })
            }
            else if (child.tagName.toUpperCase() == "OPTGROUP") {
                result.push({
                    type: 2,
                    value: child.getAttribute("value"),
                    name: child.getAttribute("name"),
                    checked: child.getAttribute("checked") !== null,
                    content: child.innerHTML,
                    nodes: this.getOptionAndOptGroup(child.innerHTML)
                })
            }
        }
        return result;
    },
    getOptGroups: function (str) {
        var result = [], 
            pattern = /<optgroup\s*([^>]*?\s*)>([\S\s]*?)<\/optgroup>/g;

        var matches = pattern.exec(str);
        while (matches) {
            var computed = this.getStrObj(matches[1]);
            computed.options = this.getOptions(matches[2]);
            result.push(computed);
            matches = pattern.exec(str);
        }
        return result;
    },
    getHref: function (str) {
        var pattern = /<a\s+([^>]*?)\s*>([\S\s]*?)<\/a>/g,
            result = [];
        var matches = pattern.exec(str);
        while (matches) {
            var computed = this.getStrObj(matches[1]);
            computed.content = matches[2];
            result.push(computed);
            matches = pattern.exec(str);
        }
        return result;
    },
    getStrObj: function (str) {
        str = str.replace(/"[^"]*"/g, function (a) {
            return '"' + encodeURIComponent(a.slice(1, -1)) + '"';
        });
        var list = str.split(/\s+/);
        var obj = {};
        for (var i = 0; i < list.length; i++) {
            if (list[i]) {
                var mats = list[i].split("=");
                var value = mats[1] ? mats[1].slice(1, -1) : mats[1], key = mats[0];
                if (value) obj[key] = decodeURIComponent(value);
                else obj[key] = true;
            }
        }
        return obj;
    }
})